package pack.findthewords;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class Toc extends Activity {
    final static private int PADDING_H=10;
    final static private int PADDING_V=10;
    final static private String HIDDENCHAR=".";

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	// On small screens, force portrait layout
	if (((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL)
	    || ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL)) {
	    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
    }

    private void generateToc() {
	SharedPreferences settings=getSharedPreferences(Utils.PREFS_NAME, 0);
	int recount=0;

	// Layouts
	RelativeLayout layout=(RelativeLayout) View.inflate(this, R.layout.toc, null);
	TextView recountTv=(TextView) layout.getChildAt(4);
	ScrollView scroll=(ScrollView) layout.getChildAt(5);
	LinearLayout scrollInnerLayout=(LinearLayout) scroll.getChildAt(0);

	// ToC
	TextView tipTv=(TextView) layout.getChildAt(1);
	int r=Utils.getRandForTip();
	final String urlTip=Utils.LOOKUP_TIPS_URLS[r];
	tipTv.setText(getResources().getString(Utils.LOOKUP_TIPS[r]));
	tipTv.setOnClickListener(new View.OnClickListener() {
	    @Override
	    public void onClick(View v) {
		Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse(urlTip));
		startActivity(i);
	    }
	});

	for (int i=0; i < Utils.LOOKUP_A.length; i++) {

	    final int level=i;
	    String a=getResources().getString(Utils.LOOKUP_A[i]);
	    String c=getResources().getString(Utils.LOOKUP_C[i]);
	    String e=getResources().getString(Utils.LOOKUP_E[i]);

	    String b=" ";
	    String d=" ";
	    String f=" ";

	    if (settings.getBoolean("lvl" + i, false)) {
		b=b + getResources().getString(Utils.LOOKUP_B[i]).toUpperCase();
		d=d + getResources().getString(Utils.LOOKUP_D[i]).toUpperCase();
		if ( ! e.equals(Utils.NULLCHAR)) {
		    f=f + getResources().getString(Utils.LOOKUP_F[i]).toUpperCase();
		}
	    }
	    else {
		b=b + getResources().getString(Utils.LOOKUP_B[i]).substring(0, 1).toUpperCase() + HIDDENCHAR;
		d=d + getResources().getString(Utils.LOOKUP_D[i]).substring(0, 1).toUpperCase() + HIDDENCHAR;
		if ( ! e.equals(Utils.NULLCHAR)) {
		    f=f + getResources().getString(Utils.LOOKUP_F[i]).substring(0, 1).toUpperCase() + HIDDENCHAR;
		}
	    }

	    String s=(i + 1) + ")  " + a + " " + b + " " + c + " " + d;
	    if ( ! e.equals(Utils.NULLCHAR)) {
		s=s + " " + e + " " + f;
	    }
	    final Button bt=new Button(this, null, android.R.attr.textAppearanceMediumInverse);

	    if (settings.getBoolean("lvl" + i, false)) {
		bt.setTextAppearance(getApplicationContext(), R.style.ListButtonStyleDone);
		recount++;
	    }
	    else {
		bt.setTextAppearance(getApplicationContext(), R.style.ListButtonStyle);
	    }
	    int scale=(int) getResources().getDisplayMetrics().density;
	    bt.setPadding(PADDING_H * scale, PADDING_V * scale, PADDING_H * scale, PADDING_V * scale);
	    bt.setText(s);
	    bt.setId(i);
	    scrollInnerLayout.addView(bt);
	    // Set listener
	    bt.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
		    Intent intent=new Intent(Toc.this, Answer.class);
		    intent.putExtra(Utils.LEVELTAG, level);
		    startActivity(intent);
		}
	    });

	}
	recountTv.setText(getResources().getString(R.string.recount) + " " + recount + "/" + Utils.LOOKUP_A.length);
	setContentView(layout);
    }

    @Override
    public void onResume() {
	super.onResume();
	generateToc();
    }

    /*
     * Options menu
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	MenuInflater inflater=getMenuInflater();
	inflater.inflate(R.menu.opt_menu_gen, menu);
	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	    case R.id.opt_vote:
		Intent ii=new Intent(Intent.ACTION_VIEW, Uri.parse(Utils.MARKET_URL));
		startActivity(ii);
		return true;
	    case R.id.opt_back:
		finish();
		return true;
		/*
		 * case R.id.opt_adfree: Intent iii=new Intent(Intent.ACTION_VIEW,
		 * Uri.parse(Utils.MARKET_URL_NOADS)); startActivity(iii); return true; case
		 * R.id.opt_info: Intent iiii=new Intent(Toc.this, Info.class); startActivity(iiii);
		 * return true;
		 */
	    default:
		return super.onOptionsItemSelected(item);
	}
    }

}